# here is application start
#pip install selenium==4.9.0
import datetime
import json

import requests
import schedule
import time

import createClassTimeTableImage
import getData

import VAR

# from flask import Flask


week_list = ["星期一","星期二","星期三","星期四","星期五","星期六","星期日"]

def toDay():
    nowWeekDay = week_list[datetime.date.today().weekday()]
    nowTimeStamp = time.time()

    file = open("./thisWeekData.txt", "r")
    jsonObject = json.load(file)

    createClassTimeTableImage.createImage(
        '{"now_week": "' + jsonObject['now_week']
        + '","day":"' + nowWeekDay + '","date":"'
        + str(nowTimeStamp)
        + '","last_update_time": "' + jsonObject['last_update_time'] + '","data":' + str(jsonObject["data"][datetime.date.today().weekday()]).replace("'", "\"") + '}')

    requests.get("http://qq_send_message.wuhupoo.cn/sendMessage?path="+VAR.IMAGE_SERVER_FILE+"/classTable.png")

def toAfternoon():
    nowWeekDay = week_list[datetime.date.today().weekday()]
    nowTimeStamp = time.time()

    file = open("./thisWeekData.txt", "r")
    jsonObject = json.load(file)

    todayClassList = jsonObject["data"][datetime.date.today().weekday()]

    afternoonClass = []
    for line in todayClassList:
        if int(line['end']) > 5: afternoonClass.append(line)


    createClassTimeTableImage.createImage(
        '{"now_week": "' + jsonObject['now_week']
        + '","day":"' + nowWeekDay + '","date":"'
        + str(nowTimeStamp)
        + '","last_update_time": "' + jsonObject['last_update_time'] + '","data":' + str(afternoonClass).replace("'", "\"") + '}')

    requests.get("http://qq_send_message.wuhupoo.cn/sendMessage?path="+VAR.IMAGE_SERVER_FILE+"/classTable.png")

def tomorrow():
    nowWeekDay = week_list[datetime.date.today().weekday()+1]

    today = datetime.date.today()
    tomorrow = today + datetime.timedelta(days=1)

    tomorrow_start_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d')))*100000

    file = open("./thisWeekData.txt", "r")
    jsonObject = json.load(file)

    createClassTimeTableImage.createImage(
        '{"now_week": "' + jsonObject['now_week']
        + '","day":"' + nowWeekDay + '","date":"'
        + str(tomorrow_start_time)
        + '","last_update_time": "' + jsonObject['last_update_time'] + '","data":' + str(jsonObject["data"][datetime.date.today().weekday()+1]).replace("'", "\"") + '}')

    requests.get("http://qq_send_message.wuhupoo.cn/sendMessage?path=" + VAR.IMAGE_SERVER_FILE + "/classTable.png")

def monday():
    nowWeekDay = week_list[0]

    today = datetime.date.today()
    tomorrow = today + datetime.timedelta(days=1)

    tomorrow_start_time = int(time.mktime(time.strptime(str(tomorrow), '%Y-%m-%d'))) * 100000

    file = open("./thisWeekData.txt", "r")
    jsonObject = json.load(file)

    createClassTimeTableImage.createImage(
        '{"now_week": "' + jsonObject['now_week']
        + '","day":"' + nowWeekDay + '","date":"'
        + str(tomorrow_start_time)
        + '","last_update_time": "' + jsonObject['last_update_time'] + '","data":' + str(jsonObject["data"][0]).replace("'", "\"") + '}')

    requests.get("http://qq_send_message.wuhupoo.cn/sendMessage?path=" + VAR.IMAGE_SERVER_FILE + "/classTable.png")

    # requests.get("http://qq_send_message.wuhupoo.cn/sendMessage?path=https://7up.pics/images/2023/10/22/__z_x_drawn_by_shiratama_shiratamaco__0256cfa69777d6f540684691161c2849.jpeg")

def morning(): toDay()
def noon(): toDay()
def evening():tomorrow()


def update():
    try:
        getData.getData(False)
    except IOError: pass

# update()
# schedule.every(40).seconds.do(morning)
# schedule.every(40).seconds.do(evening)

# update
schedule.every().day.at("08:30:00").do(update)
schedule.every().day.at("14:00:00").do(update)
schedule.every().day.at("21:30:00").do(update)
schedule.every().day.at("17:20:00").do(update)

# schedule.every().day.at("12:00:00").do(morning)
# schedule.every().day.at("12:30:00").do(noon)
# schedule.every().day.at("13:30:00").do(evening)

schedule.every().day.at("14:10:00").do(toAfternoon)
# schedule.every().day.at("12:30:00").do(noon)

def nightSendMessage():
    if datetime.date.today().weekday() == 6:
        try:
            getData.getData(False,True)
        except IOError:
            pass
        monday()
    else: evening()

schedule.every().day.at("20:30:00").do(nightSendMessage)




# app = Flask(__name__,static_folder='/static')

# @app.route("/")
# def main():
#     return "bot start"


while True:
    schedule.run_pending()
    time.sleep(1)
# if __name__ == '__main__':
#     app.run(port=8088)
#     startTimer()
